package com.wing.sys.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wing.common.util.GUID;
import com.wing.common.util.PageBean;
import com.wing.sys.dao.CodeDao;
import com.wing.sys.domain.Code;
/**
 * 
 * @author please write yout name
 * 
 */
@Service
@Transactional
public class CodeManager {
	@Autowired
	private CodeDao codedao;
	
	
	public List<Map<String,Object>> findAll(){
		return codedao.findAll();	 
	}

	public List<Map<String,Object>> findChildByCode(String code){
		return codedao.findChildByCode(code);	 
	}
	
	public PageBean findPageList(int currentPage, int pageSize, String codeId) {
		return codedao.findPageList(currentPage, pageSize, codeId);
	}
	
	public Code findById(String id){
		return codedao.findById(id);	 
	}
	public Code findByCode(String code) {
		return codedao.findByCode(code);
	}
	
	public void saveCode(Code code, String ptree_id){
		if(code.getId() == null ||  "".equals(code.getId())){
			code.setId(new GUID().toString());
			ptree_id += ";"+code.getId();
			code.setTree_id(ptree_id);
			codedao.insert(code);
		}else{
			Code c = codedao.findById(code.getId());
			//如果用户修改系统编码，不允许
			if(!c.getType().equals("sys")){
				codedao.update(code);
			}
		}
	}

	public void updateCode(Code code){
		codedao.update(code);
	}
	

	public int deleteCodes(String[] chk) {
		int count = 0;
		for(int i=0;i<chk.length;i++){
			count+=deleteCode(chk[i]);
		}
		return count;
	}
	
	/**
	 * 递归删除codeId下的所有的 编码
	 * @param codeId
	 */
	private int deleteCode(String codeId){
		int count=0;
		Code c = codedao.findById(codeId);
		if(c.getType().trim().equals("sys")){
			//系统编码不允许删除
			return 0;
		}
		List<Map<String, Object>> list = codedao.findChildByID(codeId);
		for(int i=0;i<list.size();i++){
			Map map = (Map)list.get(i);
			count+=deleteCode((String)map.get("id"));
		}
		codedao.delete(codeId);
		count++;
		return count;
	}
	
	/********************************获取编码树的xml开始*******************************
	 * @param  */
	
	public String createDHTMLXML(String parentid,boolean firstFlag) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		if (parentid.equalsIgnoreCase(Code.CODEROOT) || firstFlag) {
			Code rootCode = codedao.findById(parentid);
			buffer.append("<tree id=\"0\">");
			buffer.append("<item text=\""+rootCode.getName()+"\" id=\""+rootCode.getId()+"\" open=\"1\" select=\"1\">");
		} else {
			buffer.append("<tree id=\"" + parentid + "\">");
		}
		// 循环读取所有节点 
		List<Map<String, Object>> list = codedao.findChildByID(parentid);
		for (Map map : list) {
			String id = (String)map.get("id");
			String name = (String)map.get("name");
			buffer.append("<item text= \"" + name + "\"");
			buffer.append(" id= \"" + id + "\"");

			if (codeIsLeaf(id)) {
				buffer.append(" im0=\"iconText.gif\" im1=\"iconTexts.gif\" im2=\"iconText.gif\"");
				buffer.append(" child=\"0\" >");
			} else {
				buffer.append(" im0=\"folderClosed.gif\" im1=\"folderOpen.gif\" im2=\"folderClosed.gif\"");
				buffer.append(" child=\"" + id + "\">");
			}
			buffer.append("</item>");
		}
		if (parentid.equalsIgnoreCase(Code.CODEROOT) || firstFlag) {
			buffer.append("</item>");
		}
		buffer.append("</tree>");
		return buffer.toString();
	}
	

	/**
	 * 判断该编码是否是叶子节点
	 * @param codeanization Id
	 * @return 
	 */
	private boolean codeIsLeaf(String id){
		List list = codedao.findChildByID(id);
		return list.size()<=0;
	}
	/********************************获取编码树的xml结束********************************/

	public List findCodesByCode(String code) {
		return codedao.findCodesByCode(code);
	}
}



